package com.chushouya.controller.product.admin;

import com.chushouya.common.constant.ServiceType;
import com.chushouya.product.dto.admin.evaluatelog.EvaluateLogExportDTO;
import com.chushouya.product.dto.admin.evaluatelog.EvaluateLogListDTO;
import com.chushouya.product.dto.admin.evaluatelog.EvaluateLogQuery;
import com.chushouya.product.service.admin.EvaluateLogService;
import com.general.framework.core.schema.PageResult;
import com.general.framework.core.schema.Response;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Strings;
import com.general.framework.web.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = ServiceType.PLATFORM + "估价日志管理")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/evaluateLog")
public class EvaluateLogController {

    @Resource
    private EvaluateLogService evaluateLogService;

    @ApiOperation("分页查询估价日志列表")
    @GetMapping("/list")
    public Response<PageResult<EvaluateLogListDTO>> list(EvaluateLogQuery query) {
        return Response.success(evaluateLogService.selectEvaluateLogPageResult(query));
    }

    @ApiOperation("导出估价日志列表")
    @GetMapping("/export")
    public void export(HttpServletResponse response, EvaluateLogQuery query) {
        // 设置较大分页以导出大量数据
        query.setSize(100000);
        query.setPage(1);
        try {
            PageResult<EvaluateLogListDTO> pageResult = evaluateLogService.selectEvaluateLogPageResult(query);
            List<EvaluateLogExportDTO> exportList = Beans.copyList(pageResult.getRows(), EvaluateLogExportDTO.class);
            String fileName = Strings.format("估价日志导出-{}", Dates.dateTimeNow());
            new ExcelUtil<>(EvaluateLogExportDTO.class).exportExcel(response, exportList, fileName, "估价日志");
        } catch (Exception e) {
            throw Ex.systemError(Strings.format("估价日志导出失败: {}", e.getMessage()));
        }
    }
}
